package com.example.myapplication;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static MainActivity appRef=null;
    private Button callServiceBtn;
    private Button exitServiceBtn;
    boolean k=false;
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        appRef=this;
        setContentView(R.layout.activity_main);
        callServiceBtn=(Button)findViewById(R.id.cal_alarm);
        callServiceBtn.setOnClickListener(this);
        exitServiceBtn=(Button)findViewById(R.id.exit);
        exitServiceBtn.setOnClickListener(this);
    }
    public static MainActivity getApp(){return appRef;}
    public void btEvent(String data){setTitle(data);}
    public void onClick(View view){
        //放到外面试试
        Intent intent=new Intent(MainActivity.this,AlarmReceiver.class);
        PendingIntent p_intent=PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
        AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
        if(view==callServiceBtn){
            setTitle("Waiting.");
            //获取当前时间
            Calendar calendar=Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            //设置唤醒时间
            calendar.add(Calendar.SECOND,1);
            //参数1：设置闹钟类型；参数2：执行时间；参数3：唤醒响应动作
            am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p_intent);
            //am.cancel(p_intent);
        }
        if(view==exitServiceBtn){
            am.cancel(p_intent);
            finish();
        }
    }
}
